<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item>
					<u-avatar :src="src" size="80"></u-avatar>
				</u-form-item>
				<u-form-item label="昵称">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="value" @change="radioGroupChange">
								<u-radio 
									@change="radioChange" 
									v-for="(item, index) in sexList" :key="index" 
									:name="item.name"
									:disabled="item.disabled"
								>
									{{item.name}}
								</u-radio>
							</u-radio-group>
				</u-form-item>
				<u-form-item label="账号">
					<u-input v-model="form.userName"></u-input>
				</u-form-item>
				<u-form-item label="密码">
					<u-input v-model="form.password"></u-input>
				</u-form-item>
				<u-form-item label="确认密码" prop="repassword">
					<u-input v-model="form.repassword"></u-input>
				</u-form-item>
				<u-form-item label="身份证号">
					<u-input v-model="form.idCard"></u-input>
				</u-form-item>
				<u-form-item label="手机号">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email"></u-input>
				</u-form-item>
			</u-form>
			<button @click="submit">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				sexList:[
					{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				value: '男',
				form: {
					avatar: '',
					userName: '',
					nickName: '',
					password: '',
					repassword: '',
					phonenumber: '',
					sex: '',
					email: '',
					idCard: ''
				},
				rules: {
					repassword: [
						{
							validator: (rule,value,callback) => {
								if(value !== this.form.password){
									callback(new Error('密码输入不一致'))
								}else{
									callback()
								}
							},
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		methods: {
			radioGroupChange(e){
				if(e === '男'){
					this.form.sex = '0'
				}else{
					this.form.sex = '1'
				}
			},
			radioChange(e){
				if(e === '男'){
					this.form.sex = '0'
				}else{
					this.form.sex = '1'
				}
			},
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/register',
							method: 'POST',
							data: {
								...this.form
							}
						}).then(res => {
							if(res.code === 200){
								uni.showToast({
									title:'注册成功',
								})
								uni.navigateTo({
									url:'/pages/login/login'
								})
							}else{
								uni.showToast({
									title:res.msg,
									icon: 'error'
								})
							}
						})
					}else{
						uni.showToast({
							title:'两次密码输入不一致',
							icon: 'error'
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
.main{
	padding: 20rpx;
	button{
		width: 240rpx;
		height: 80rpx;
		font-size: 28rpx;
		background-color: #2979ff;
		color: white;
		text-align: center;
		margin-top: 50rpx;
	}
}
</style>
